package com.fary.typehandle;

import cn.hutool.core.util.StrUtil;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.springframework.stereotype.Component;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 对请求参数，返回结果 去掉前后空格
 */
@Component
@MappedTypes(String.class)
@MappedJdbcTypes({JdbcType.CHAR, JdbcType.VARCHAR})
public class StringTrimTypeHandler extends BaseTypeHandler<String> {

    public void setNonNullParameter(PreparedStatement preparedStatement, int i, String str, JdbcType jdbcType) throws SQLException {
        preparedStatement.setString(i, StrUtil.trim(str));
    }

    public String getNullableResult(ResultSet resultSet, String column) throws SQLException {
        return StrUtil.trimEnd(resultSet.getString(column));
    }

    public String getNullableResult(ResultSet resultSet, int i) throws SQLException {
        return StrUtil.trimEnd(resultSet.getString(i));
    }

    public String getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        return StrUtil.trimEnd(callableStatement.getString(i));
    }

}
